import React, { PureComponent, Fragment } from 'react';
import moment from 'moment/moment';
import {
  Form,
  Input,
  Select,
  DatePicker,
  Modal,
  Radio, Tooltip, Icon,
} from 'antd';
import { phone, email } from '@/utils/regular';

const FormItem = Form.Item;
const { TextArea } = Input;
const { Option } = Select;
const dateFormat = 'YYYY-MM-DD';

const RadioGroup = Radio.Group;

//更新和新增用户信息   modalVisible

@Form.create()
class UpdateForm extends PureComponent {
  constructor(props) {
    super(props);
    this.formLayout = {
      labelCol: { span: 7 },
      wrapperCol: { span: 13 },
    };
  }

  // 表单
  renderContent = () => {
    const { form, values = {}, modalVisible } = this.props;
    let phone = modalVisible ? <FormItem key="mobile"   {...this.formLayout} label="手机号">
      {form.getFieldDecorator('mobile', {
        rules: [
          { required: true, message: '请输入手机号！' },
          { pattern: phone, message: '请输入正确手机号' },
        ],
        initialValue: values.mobile,
      })(<Input placeholder="请输入手机号"/>)}
    </FormItem> : '';
    return [
      <FormItem key="entUserName" {...this.formLayout} label="用户名称">
        {form.getFieldDecorator('entUserName', {
          rules: [{ required: true, message: '请输入用户名称！' }],
          initialValue: values.userName,
        })(<Input placeholder="请输入用户名称"/>)}
      </FormItem>,
      <FormItem key="pwd" {...this.formLayout} label={<Tooltip title="8-18位，至少包含字母（区分大小写）、数字、符号中的三种">
        密码<Icon style={{ color: 'rgba(0, 0, 0, 0.43)', marginLeft: 4 }} type="info-circle-o"/>
      </Tooltip>}>
        {form.getFieldDecorator('pwd', {
          rules: [{ required: modalVisible, message: '请输入密码！' }],
        })(<Input type={'password'} autocomplete='new-password' placeholder="请输入密码"/>)}
      </FormItem>,
      phone,
      <FormItem key="status" {...this.formLayout} label="状态">
        {form.getFieldDecorator('status', {
          rules: [{ required: true, message: '请选择状态！' }],
          initialValue: JSON.stringify(values.status),
        })(
          <RadioGroup>
            <Radio value="1">启用</Radio>
            <Radio value="2">停用</Radio>
          </RadioGroup>,
        )}
      </FormItem>,
    ];
  };

  //弹窗
  render() {
    const { updateModalVisible, handleUpdateModalVisible, modalVisible, form } = this.props;
    //完成
    const handleNext = () => {
      const { values, handleUpdate, handleAdd, modalVisible } = this.props;
      form.validateFields((err, fieldsValue) => {
        if (err) return;
        const formVals = { ...values, ...fieldsValue };
        formVals.status = Number(formVals.status);

        if (modalVisible) {
          handleAdd(formVals);
        } else {
          handleUpdate(formVals);
        }
        form.resetFields();
      });
    };
    const onCancel = () => {
      handleUpdateModalVisible(false, []);
      form.resetFields();
    };
    return (
      <Modal
        width={640}
        bodyStyle={{ padding: '32px 40px 48px' }}
        destroyOnClose
        title={modalVisible ? '新增账户' : '修改账户'}
        visible={updateModalVisible}
        onOk={handleNext}
        onCancel={onCancel}
      >
        {this.renderContent()}
      </Modal>
    );
  }
}

export default UpdateForm;
